package org.xukai.common;

import com.google.common.base.Charsets;
import org.apache.commons.lang.StringUtils;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

/**
 * @author xukai
 * @desc
 * @date 2017-05-17-18:08
 */
public class CvsUtil {

    public static <T> List<T> cvs2BeanList(byte[] buff, Class<T> cls) {
        CvsFieldslNum annotation = cls.getAnnotation(CvsFieldslNum.class);
        int fieldsNum = 0;
        if (annotation != null) {
            fieldsNum = annotation.value();
        }
        Method[] methods = cls.getDeclaredMethods();
        HashMap<Integer, Method> invokeMethodMapping = new HashMap<>();
        for (Method method : methods) {
            if (method.isAnnotationPresent(CvsSerialNum.class)) {
                CvsSerialNum cvsSerialNum = method.getAnnotation(CvsSerialNum.class);
                invokeMethodMapping.put((cvsSerialNum.value()), method);
            }
        }
        BufferedReader br = null;
        ArrayList<T> list = new ArrayList<>();
        try {
            br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(buff), Charsets.UTF_8.name()));
            // 要阅读的最后一行


            boolean notFieldName = false;

            while (br.ready()) {
                String line = br.readLine();
                int count = 0;
                // 数据元素分为一行
                T t = cls.newInstance();
                Scanner scanner = new Scanner(line).useDelimiter("\t");

                while (scanner.hasNext()) {
                    // 每个元素都显示在一个制表符分隔的线
                    String fieldValue = scanner.next();
                    if (StringUtils.isBlank(fieldValue)) {
                        count++;
                        continue;
                    }
                    try {
                        invokeMethodMapping.get(count % fieldsNum).invoke(t, fieldValue);
                        if ((count % fieldsNum) == 0) {
                            System.out.println(fieldValue);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    count++;
                }
                if (notFieldName) {
                    list.add(t);
                }
                notFieldName = true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;

    }

    private static boolean keepRunning = true;

    public static void main(String[] args) throws Exception {

        new Thread(
                () -> {

                    while (keepRunning) {
                        System.out.println("cvv");
                    }
                }

        ).start();

        Thread.sleep(1000);
        keepRunning = false;


    }
}
